﻿using CardMonitor.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using DotNetSpeech;
using CardMonitor.Pages;
using CardMonitor.Windows;
using CardMonitor.Model;
using Newtonsoft.Json.Linq;

namespace CardMonitor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private DispatcherTimer timer;
        private String TextProperty1 = String.Empty;
        private SpVoice speech = new SpVoice();
        private MainWindow mainWindow;
        private IntPtr icdev = IntPtr.Zero;

        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += new EventHandler(timeCycle);
            timer.Start();

            mainWindow = this;

            this.Loaded += Window_Load;
            this.Unloaded += Window_UnLoad;
        }

        private void Window_Load(object sender, EventArgs e)
        {
            string cardReader = App.getConfig().cardReader;

            if ("1".Equals(cardReader))//精伦读卡器
            {
                int code = NewIDcardread.Opendevice();//打开读卡设备
                if (code != 1)
                {
                    MessageBox.Show("打开设备失败");
                }
            }
            else if ("2".Equals(cardReader)) //熵机
            {
                string IDCardPort = App.getConfig().SJIDCardPort;
                int inport = Convert.ToInt32(IDCardPort);
                bool inPortType = IDCardReaderUtil_Sjdll.InitDevice(inport);
                if (!inPortType)
                {
                    MessageBox.Show("熵机读卡器设备打开失败");
                }
            }

            Index indexPage = new Index(this);
            //indexPage.getIcPtr(icdev);
            frmMain.Content = indexPage;
        }

        private void Window_UnLoad(object sender, EventArgs e)
        {
            string sMsg = string.Empty;
            if ("1".Equals(App.getConfig().IDCardDevice))
            {
                try
                {
                    string cardReader = App.getConfig().cardReader;

                    if ("1".Equals(cardReader))//精伦读卡器
                    {
                        int ret = NewIDcardread.closeID();//关闭读卡器
                        if (ret != 1)
                        {
                            return;
                        }
                    }
                    else if ("2".Equals(cardReader)) //熵机
                    {
                        bool code = IDCardReaderUtil_Sjdll.CloseDevice();
                        if (!code)
                        {
                            MessageBox.Show("设备关闭失败");
                        }
                    }
                }
                catch { }
            }
            try
            {
                if (App.getGlobalVariables().cardDeviceComm.PortState)
                {
                    App.getGlobalVariables().cardDeviceComm.ClosePort();
                }
                //if (App.getGlobalVariables().cardComm.PortState)
                //{
                //    //App.getGlobalVariables().cardComm.ClosePort();
                //    Reader.hc_exit(icdev);
                //}
            }
            catch { }
        }

        public void timeCycle(object sender, EventArgs e)
        {
            new Thread(() =>
            {
                Application.Current.Dispatcher.BeginInvoke(
                    new Action(() =>
                    {
                        TextProperty1 = DateTime.Now.ToString();
                        this.labelTime.Content = TextProperty1;
                    }));
            }).Start();
        }

        public void Speak(String message)
        {
            speech.Speak(message, SpeechVoiceSpeakFlags.SVSFlagsAsync);
        }

        public void BreakSpeak()
        {
            speech.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
        }

        private void imgLogo_MouseUp(object sender, MouseButtonEventArgs e)//logo点击事件
        {
            PasswordWindow passwordWindow = new PasswordWindow();
            passwordWindow.Owner = mainWindow;
            bool result = (bool)passwordWindow.ShowDialog();
            if (!result)
            {
                return;
            }

            SettingWindow win = new SettingWindow();
            win.Owner = mainWindow;
            result = (bool)win.ShowDialog();
            if (result)
            {
                Application.Current.Shutdown();
            }
        }

        private void frmMain_Navigated(object sender, NavigationEventArgs e)
        {
        }

        private int checkCard(JObject paramObj)
        {
            ReadCardWindow readcardWindow = new ReadCardWindow();

            bool result = (bool)readcardWindow.ShowDialog();
            if (!result)
            {
                return -1;
            }
            String guidCode = paramObj["guidCode"].ToString();
            if (!guidCode.Equals(readcardWindow.billModel.FacId))
            {
                return -2;
            }
            String carNum = paramObj["carNumber"].ToString();
            if (!carNum.Equals(readcardWindow.billModel.CarNumber))
            {
                return -3;
            }
            String control = paramObj["control"].ToString();
            if (!control.Equals(readcardWindow.billModel.ProdCode))
            {
                return -4;
            }
            String billCode = paramObj["billCode"].ToString();
            if (!billCode.Equals(readcardWindow.billModel.Code))
            {
                return -5;
            }
            String ynum = paramObj["ynum"].ToString();
            if (!ynum.Equals(readcardWindow.planNum))
            {
                return -6;
            }

            return 1;
        }
    }
}